package com.leo.javase.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Serializable;

import org.junit.Test;

public class IOTest {
	/**
	 * 文件大小：6KB
	 * 执行时间(ms):209
	 */
	@Test
	public void testPrintHex(){
		try{
			long start = System.currentTimeMillis();
			String file = "E:/temp_code/tmp.txt";
			IOUtils.printHex(file);
			System.out.println("执行时间(ms):"+(System.currentTimeMillis()-start));
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * 文件大小：6KB
	 * 执行时间(ms):187
	 */
	@Test
	public void testPrintHexByByteArray(){
		try{
			long start = System.currentTimeMillis();
			String file = "E:/temp_code/tmp.txt";
			IOUtils.printHexByByteArray(file);
			System.out.println("执行时间(ms):"+(System.currentTimeMillis()-start));
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * 测试文件的复制
	 */
	@Test
	public void testCopyFile(){
		try{
			File srcFile = new File("E:/temp_code/tmp.txt");
			File destFile = new File("E:/temp_code/tmp1.txt");
			IOUtils.copyFileByStream(srcFile, destFile);
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * 测试DataOutputStream
	 */
	@Test
	public void testDataOutpuStream()throws Exception{
		String file = "E:/temp_code/dos.dat";
		DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
		dos.writeInt(10);
		dos.writeInt(-10);
		dos.writeDouble(10.5);
		dos.writeUTF("练亮");
		dos.close();
		IOUtils.printHex(file);
	}
	/**
	 * 测试DataInputStream
	 */
	@Test
	public void testDataInputStream()throws Exception{
		String file = "E:/temp_code/dos.dat";
		DataInputStream in = new DataInputStream(new FileInputStream(file));
		System.out.println(in.readInt());
		System.out.println(in.readInt());
		System.out.println(in.readDouble());
		System.out.println(in.readUTF());
		in.close();
	}
	/**
	 * 测试两种复制文件的方式
	 */
	@Test
	public void testTwoCopyFile(){
		try{
			File srcFile = new File("E:/temp_code/005.mp4");
			File destFile = new File("E:/temp_code/005-2.mp4");
			
			long start = System.currentTimeMillis();
			//耗时：780
			//IOUtils.copyFileByStream(srcFile, destFile); 
			//耗时：150
			IOUtils.copyFileByBuffered(srcFile, destFile); //30
			
			System.out.println(System.currentTimeMillis() - start);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * 读取单个字符
	 * @throws Exception
	 */
	@Test
	public void testReader() throws Exception{
		String file = "E:/temp_code/tmp1.txt";
		InputStreamReader in = new InputStreamReader(new FileInputStream(file),"GBK");
		int c = 0;
		while((c = in.read())!=-1){
			System.out.print((char)c);
		}
		in.close();
	}
	@Test
	public void testReaderArray(){
		try{
			String file = "E:/temp_code/tmp1.txt";
			InputStreamReader in = new InputStreamReader(new FileInputStream(file),"GBK");
			char[] buf = new char[8 * 1024];
			int c = 0;
			String s ;
			while((c = in.read(buf, 0, buf.length))!=-1){
				s = new String(buf, 0, c);
				System.out.println(s);
			}
			in.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * 测试-字符缓冲流(包括 字符输入流，字符输出流)，PrintWriter
	 */
	@Test
	public void testReaderWrite()throws Exception {
		File file1 = new File("E:/temp_code/tmp1.txt");
		File file2 = new File("E:/temp_code/tmp3.txt");
		BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file1),"GBK"));
		//BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file2)));
		PrintWriter out = new PrintWriter(file2);
		String line;
		while((line = in.readLine())!=null){
			System.out.println(line);
			/*out.write(line);
			out.newLine();
			out.flush();*/
			out.println(line);
			out.flush();
		}
		in.close();
		out.close();
	}
	/**
	 * 测试序列化-往文件里面序列化数据
	 */
	@Test
	public void testOutSerializable()throws Exception{
		File file = new File("E:/temp_code/serial.txt");
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
		//Student stu = new Student("S001","Leo",24);
		//Foo2 foo2 = new Foo2();
		Bar2 bar2 = new Bar2();
		out.writeObject(bar2);
		out.close();
	}
	/**
	 * 测试反序列化-从文件里面读取对象
	 * @throws Exception
	 */
	@Test
	public void testInSerializable()throws Exception{
		File file = new File("E:/temp_code/serial.txt");
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
		//Student stu = (Student) in.readObject();
		//Foo2 foo2 = (Foo2) in.readObject();
		Bar2 bar2 = (Bar2) in.readObject();
		System.out.println(bar2);
	}
	
}
//Bar类族
class Bar{
	public Bar(){
		System.out.println("...Bar...");
	}
}
class Bar1 extends Bar implements Serializable{
	public Bar1(){
		System.out.println("...Bar1...");
	}
}
class Bar2 extends Bar1{
	public Bar2(){
		System.out.println("...Bar2...");
	}
}
//Foo 类族
class Foo implements Serializable{
	public Foo(){
		System.out.println("...Foo...");
	}
}
class Foo1 extends Foo{
	public Foo1(){
		System.out.println("...Foo1...");
	}
}
class Foo2 extends Foo1{
	public Foo2(){
		System.out.println("...Foo2...");
	}
}
